package com.njupt.DoublePoint;

/**
 * @Author: wujiaming
 * @CreateTime: 2025/2/25 21:53
 * @Description: 11. 盛最多水的容器
 * @Version: 1.0
 */


public class MaxArea_11 {


    /**
     * 暴力法 超时
     * @param height
     * @return
     */
    public int maxArea1(int[] height) {
        int maxArea = 0;
        for (int i = 0; i < height.length-1; i++) {
            for (int j = i+1; j < height.length; j++) {
                int width = j-i;
                int he = Math.min(height[i],height[j]);
                int area = width * he;
                if(area > maxArea){
                    maxArea = area;
                }
            }
        }
        return maxArea;
    }

    /**
     * 双指针法，left++ 与 right--，都是为了取得更多的水，如果短的半不动的话每次取得的水都不会比上一次大
     * @param height
     * @return
     */
    public int maxArea(int[] height) {
        int left = 0;
        int right = height.length -1;
        int maxArea = 0;
        while(left < right){
            int with = right - left;
            int he = Math.min(height[left],height[right]);
            if(with * he > maxArea){
                maxArea = with * he;
            }
            if(height[left] < height[right]){
                left++;
            }else {
                right--;
            }
        }
        return maxArea;
    }

    public static void main(String[] args) {
        MaxArea_11 test = new MaxArea_11();
        int[] height = {1,1};
        System.out.println(test.maxArea(height));
    }
}
